﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.FrmKP
{
    /// <summary>
    /// 回写/修正税控号码-整单
    /// </summary>
    public partial class FrmReturn_SK_Red : Form
    {
        private int _id;


        public FrmReturn_SK_Red(int id)
        {
            InitializeComponent();
            this.Icon = YTSK.Properties.Resources.Fly;
            _id = id;
            SetFPHM();
            setLabel();
        }
        private void SetFPHM()
        {
            var url = string.Format("{0}/api/oms/SKSalebillVouch/GetNextVouchCode?token={1}",
                            ServerConfig.ServerAddress, ServerConfig.GetToken());
            var dic = new Dictionary<string, object>();
            dic.Add("IDSK", _id);
            //dic.Add("cBillType", _rd.cBillType);
            var s = Common.handleHttp.Post(dic, url);
            var result = Common.handleJSON.DeserializeResult(s);
            if (result.result != 1) { return; }
            this.fpdm_Ori.Text = result.value["fpdm_ori"].ObjToString();
            this.fphm_Ori.Text = result.value["fphm_ori"].ObjToString();
            this.dtp_Ori.Value = result.value["ddate_ori"].ObjToDate();
            //var fpdmsave = result.value["fpdm"].ObjToString();
            //var fphmsave = result.value["fphm"].ObjToString();
            //this.tbfpdm.Text = fpdmsave;
            //this.tbfphm.Text = fphmsave;

        }

        #region 设置label的信息
        private void setLabel()
        {
            this.dateTimePicker1.Enabled = true;
            this.Text = "发票冲红字--跨月";
            this.btOK.Text = "确认红冲";
        }
        #endregion


        #region 回写发票
        private void btOK_Click(object sender, EventArgs e)
        {
            try
            {
                var url = string.Format("{0}/api/oms/SKSalebillVouch/RedVouch?token={1}",
                         ServerConfig.ServerAddress, ServerConfig.GetToken());
                var pos = new Dictionary<string, object>();

                pos.Add("ID", _id);
                string fpdm = this.tbfpdm.Text.Trim();
                string fphm = this.tbfphm.Text.Trim();
                var err = CheckFP(fpdm, fphm);
                if (err.Length > 0) { MessageBox.Show(err); return; }
                pos.Add("fpdm", fpdm);
                pos.Add("fphm", fphm);
                var ddate = this.dateTimePicker1.Value.Date;
                pos.Add("dFPDate", ddate);

                var s = Common.handleHttp.Post(pos, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show("回写发票号出现错误" + result.resultdetail); return; }
                //MessageBox.Show("发票回写成功");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 检查发票号码及发票代码的长度
        private string CheckFP(string fpdm, string fphm)
        {
            var err = "";
            //统一为8位
            if (fphm.Length != 8)
            {
                err += string.Format("发票号码的长度必须为8位，[{0}]的长度[{1}]位不正确", fphm, fphm.Length);
            }
            //电子发票12位，普通发票和专用发票10位
            if (fpdm.Length != 10 && fpdm.Length != 12)
            {
                err += string.Format("发票代码的长度必须为10或12位，[{0}]的长度[{1}]位不正确", fpdm, fpdm.Length);
            }
            return err;
        }
        #endregion


        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

}
